package org.jeecg.modules.strategy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.strategy.model.entity.AnalyticsEntity;
import org.jeecg.modules.strategy.model.entity.AnalyticsProcessEntity;
import org.jeecg.modules.strategy.model.entity.AnalyticsResultEntity;
import org.jeecg.modules.strategy.model.query.AnalyticsProcessQuery;
import org.jeecg.modules.strategy.model.query.AnalyticsQuery;
import org.jeecg.modules.strategy.model.query.AnalyticsResultQuery;
import org.jeecg.modules.strategy.service.AnalyticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/analytics/task")
@Api(tags = "页面分析任务")
public class AnalyticsController {
    private final AnalyticsService analyticsService;

    @Autowired
    public AnalyticsController(AnalyticsService analyticsService) {
        this.analyticsService = analyticsService;
    }

    @PostMapping("/startCollectMetaTask")
    @ApiOperation(value = "开启分析任务")
    public Result<String> startCollectMetaTask(@RequestBody @Validated AnalyticsQuery analytics) {
        return analyticsService.startCollectMetaTask(analytics);
    }

    @GetMapping("listAnalyticsTask")
    @ApiOperation(value = "查询任务列表")
    public Result<?> listAnalyticsTask() {
        List<AnalyticsEntity> analyticsEntityList = analyticsService.listAnalyticsTask();
        if (analyticsEntityList == null)
            return Result.error("暂无分析任务");
        else
            return Result.ok(analyticsEntityList);
    }

    @PostMapping("/getAnalyticsProcess")
    @ApiOperation(value = "查询任务进度")
    public Result<?> getAnalyticsProcess(@RequestBody @Validated AnalyticsProcessQuery analyticsProcessQuery) {
        AnalyticsProcessEntity analyticsProcess = analyticsService.getAnalyticsProcess(analyticsProcessQuery);
        if (analyticsProcess == null)
            return Result.error("暂无分析进度");
        else
            return Result.ok(analyticsProcess);
    }

    @PostMapping("/getAnalyticsResult")
    @ApiOperation(value = "查询任务结果")
    public Result<?> getAnalyticsResult(@RequestBody @Validated AnalyticsResultQuery analyticsResultQuery) {
        List<AnalyticsResultEntity> analyticsResult = analyticsService.getAnalyticsResult(analyticsResultQuery);
        if (analyticsResult == null)
            return Result.error("暂无分析结果");
        else
            return Result.ok(analyticsResult);
    }
}